import lineApi from '../../api/line'
import orderApi from '../../api/order'
const FormData = require('../../utils/formData/formData')
Page({
    data:{
        lineList:[],
        lineId:null,
        sourceData:[],
        total:0,
        current:1,
        pages:0,//总页数
        loading:false,
        signatureShow:false,
        moreLoading:false,//上拉加载
        allFlg:false,//上拉到底了
        orderId:null,
    },
    onLoad(data) {
      this.lineList()
    },
   /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
   
   
    },
    lineList() {
      lineApi.lineList().then((res)=>{
        this.setData({
          lineList:res
        })
        this.lineId = res[0].id
        this.orderList(true)
      })
    },
    orderList(flg) {
      let data = {
        pageSize:10,
        pageNum:this.data.current,
        lineId: this.lineId
      }
      lineApi.orderList(data).then((res)=>{
        let records = res.records
             //下拉
      if(flg) {
        this.setData({
          loading:false,
        })
      }else {
        //上拉
        records = this.data.list.concat(...records)
        this.setData({
          moreLoading:false,
        })
      }
      this.setData({
        sourceData:records,
        total:res.total,
        pages:res.pages,
        allFlg:false
      })

      // 隐藏导航栏加载框  
      wx.hideNavigationBarLoading();
      // 停止下拉动作  
      wx.stopPullDownRefresh();
      })
   
    },
    goLineBtn() {
      wx.navigateTo({
        url: '../line/line'
      })
    },
    onNavigationBtn(e) {
      //导航
    },
    onPickUp(e) {
      //确认取件
      let item = e.target.dataset.item
      orderApi.orderConfirmFetch(item.orderId).then(()=>{
        wx.showToast({
          title: '取件成功!',
          icon: "none",
          duration: 500
        })
        setTimeout(()=>{
          this.onPullDownRefresh()
        },500)

        // this.orderList(true)
      })
    },
    onClick(e) {
      //确认签收
      this.setData({
        signatureShow:true,
        orderId: e.target.dataset.item.orderId
      })
    },
    onPullDownRefresh() {
      wx.showNavigationBarLoading();
      this.setData({
        loading:true,
        current:1,
        allFlg:false
      })
      this.orderList(true)
    },
    onReachBottom() {
      if (!this.data.loading&&this.data.current < this.data.pages) {
        let current = this.data.current
        current = current + 1
        this.setData({
          current,
          moreLoading:true
        })
        this.orderList(false)
      }else {
        this.setData({
          allFlg:true
        })
      }
    },
    handlChangeCanvasShow(e){
      this.handlCanvasCloseBtn()
      let src = e.detail.src
      let formData = new FormData()
      formData.appendFile("file", src);
      let data = formData.getData();
      orderApi.orderConfirmSign(data,this.data.orderId).then(()=>{
        wx.showToast({
          title: '签收成功!',
          icon: "none",
          duration: 500
        })
        setTimeout(()=>{
          this.onPullDownRefresh()
        },500)
      })
    
    },
    handlCanvasCloseBtn() {
      this.setData({
        signatureShow:false
      })
    },
})